package org.kl.bf.service.basic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.DataRole;
import org.kl.bf.repository.basic.DataRoleDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class DataRoleService {

	private DataRoleDao dataRoleDao;
	private JdbcTemplate jdbcTemp;

	@Resource
	public void setDataSource(DataSource dataSource) {
		jdbcTemp = new JdbcTemplate(dataSource);
	}

	public DataRole getDataRole(String guid) {
		return dataRoleDao.findOne(guid);
	}

	public void saveDataRole(DataRole entity) {
		dataRoleDao.save(entity);
	}

	/**
	 * 添加数据角色内容
	 * 
	 * @param dataRoleId
	 * @param dataDetailIds
	 */
	public void addDataDetails(String dataRoleId, String dataDetailIds) {
		DataRole dr = dataRoleDao.findOne(dataRoleId);
		if (dr != null) {
			String[] ddIds = dataDetailIds.split(",");
			StringBuilder sql = new StringBuilder("delete t from basic_DATA_ROLE_RIGHT t where t.data_role_id ='");
			sql.append(dataRoleId + "'");
			jdbcTemp.execute(sql.toString());
			for (String ddId : ddIds) {
				if (StringUtils.isNotBlank(ddId)) {
					sql = new StringBuilder("insert into basic_DATA_ROLE_RIGHT  values('" + dataRoleId + "','" + ddId + "')");
					jdbcTemp.update(sql.toString());
				}
			}
		}
		dataRoleDao.save(dr);
	}

	public List<String> getDataDetailList(String dataRoleId) {
		final List<String> result = new ArrayList<String>();
		StringBuilder sql = new StringBuilder("select t.DATADETAIL_ID from basic_DATA_ROLE_RIGHT  t where t.data_role_id ='");
		sql.append(dataRoleId + "'");
		jdbcTemp.query(sql.toString(), new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				result.add(rs.getString("DATADETAIL_ID"));
			}
		});
		return result;
	}

	public void deleteDataRole(String guid) {
		dataRoleDao.delete(guid);
	}

	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				dataRoleDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}

	public List<DataRole> getAllDataRole(Map<String, Object> searchParams, String sortType, String orderType) {
		Sort sort = CRUDUtils.buildSort(sortType, orderType);
		Specification<DataRole> spec = buildSpecification(searchParams);
		List<DataRole> result = dataRoleDao.findAll(spec, sort);
		return result;
	}

	public Page<DataRole> getUserDataRole(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType, String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<DataRole> spec = buildSpecification(searchParams);
		return dataRoleDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<DataRole> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<DataRole> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), DataRole.class);
		return spec;
	}

	@Autowired
	public void setDataRoleDao(DataRoleDao dataRoleDao) {
		this.dataRoleDao = dataRoleDao;
	}

}
